#!/usr/bin/env/ python
# -*- coding: utf-8 -*-

"""
@author: super
@software: PyCharm
@file: managerSystem
@time: 2021/10/20 14:15
"""

import student

class StudentManager(object):

    def __init__(self):
        self.students = []

    def student_run(self):
        self.load()
        while True:
                self.show_manu()
                try :
                    number = int(input("请输入数字："))
                except Exception as e:
                    print("请输入数字!")
                    self.student_run()
                if number == 1 :
                    self.add_student()
                elif number == 2:
                    self.delete_student()
                elif number == 3:
                    self.update_student()
                elif number == 4:
                    self.select_student()
                elif number == 5:
                    self.selectAll_student()
                elif number == 6:
                    self.save_student()
                elif number == 7:
                    exit(0)
                else:
                    print("请重新输入")
                    continue


    @staticmethod
    def show_manu():
        '''显示功能菜单'''
        print("请选择具体的操作：")
        print("1.添加学员")
        print("2.删除学员")
        print("3.修改学员")
        print("4.查询学员")
        print("5.显示所有学员信息")
        print("6.保存学员信息")
        print("7.退出系统")

    def load(self):
        '''加载学员信息'''
        try:
            f = open("student.txt", "r", encoding = "utf-8")
            list = eval(f.read())
            self.students = [student.Student(i["name"], i["sex"], i["phone"]) for i in list]
            new_list = [i.__dict__ for i in self.students]
            print(new_list)

        except Exception as e:
            raise e
        finally:
            f.close()

    def add_student(self):
        '''添加学员'''
        name = input("请输入姓名：")
        for i in self.students:
            if i.name == name:
                print("输入姓名重复,请重新输入！")
                return
        sex = input("请输入性别：")
        phone = input("请输入手机号：")
        stu = student.Student(name, sex, phone)
        self.students.append(stu)
        print("新增成功！")



    def delete_student(self):
        '''删除学员'''
        name = input("请输入姓名：")
        for i in self.students:
            if i.name == name:
                self.students.remove(i)
                print("删除成功")
                break
        else:
            print("查无此人！")


    def update_student(self):
        '''修改学员'''
        name = input("请输入要修改的学员姓名：")
        for i in self.students:
            if i.name == name:
                sex = input("请输入性别：")
                phone = input("请输入手机号：")
                if sex == "":
                    sex = i.sex
                if phone == "" :
                    phone = i.phone
                i.name = name
                i.sex = sex
                i.phone = phone
                print("修改成功")
                break
        else:
            print("输入的姓名不存在，请重新输入！")
            self.update_student()


    def select_student(self):
        '''查询学员'''
        name = input("请输入要查询的学员姓名：")
        for i in self.students:
            if i.name == name:
                print(f"学员姓名：{i.name},学员性别：{i.sex},学员手机号：{i.phone}")
                break
        else:
            print("您输入的学员姓名不存在，请重新输入！")
            self.select_student()

    def selectAll_student(self):
        '''查询所有学员'''
        for i in self.students:
            print(f"学员姓名：{i.name},学员性别：{i.sex},学员手机号：{i.phone}")

    def save_student(self):
        '''保存学员信息'''
        try:
            f = open("student.txt", "w",encoding = "utf-8")
            new_list = [i.__dict__ for i in self.students]
            f.write(str(new_list))
        except Exception as e:
            raise e
        finally:
            f.close()

